<?php

namespace App\Services\Core\Traits;

use GuzzleHttp\Client;

trait Http
{
    public $httpClient;

    private string $host = '';

    public string $wechatHost = 'https://api.weixin.qq.com/';


    public function curl(string $target = 'wechat')
    {
        if (!$this->httpClient) $this->httpClient = new Client();

        switch ($target) {
            case 'wechat' :
                $this->host = $this->wechatHost;
                break;
        }
        return $this;
    }

    public function get(string $url, array $query = []): array
    {
        return json_decode($this->httpClient->request('GET', $this->host . $url, $query)->getBody()->getContents(), true);
    }

    public function post(string $url, array $query = []): array
    {
        return json_decode($this->httpClient->request('POST', $this->host . $url, $query)->getBody()->getContents(), true);
    }

}
